﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WebSocketSharp.Server;

namespace Example3
{
    internal class BaseRequest
    {
        private HttpRequestEventArgs _httpRequestEvent;
        protected BaseRequest(HttpRequestEventArgs args)
        {
            this._httpRequestEvent = args;
            this._httpRequestEvent.Response.SetHeader("Server", "Wutos");
        }
 
        protected virtual string UrlPath { get => HttpUtility.UrlDecode(_httpRequestEvent.Request.RawUrl); }
        public static BaseRequest Create(HttpRequestEventArgs ee)
       
        {
            var path = ee.Request.RawUrl;
            string ext=Path.GetExtension(path).Trim();
            Console.WriteLine($"请求:{path}");
            if (string.IsNullOrEmpty(ext))
            {
                if (path == "/") return new HomePageRequest(ee);
                else return new HTMLPageRequest(ee);
            }
            else
            {
                switch (ext)
                {
                    case ".css": return new CSSRequest(ee);
                    case ".js": return new JavaScriptRequest(ee);
                    case ".svg": return new SVGRequest(ee);
                    case ".ico": return new ICORequest(ee);
                    default: return new NotFoundPageRequest(ee);
                }
            }
        }

        public virtual byte[] GetResponseContent()
        {
            byte[] contents;
            bool ret = _httpRequestEvent.TryReadFile(UrlPath, out contents);
            if (ret == true) return contents;
            return null;
        }
    }
}
